﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Restaurant.CoreType.Enums;
using Sector7.Restaurant.Modules.REST.Utility;
using Sector7.Restaurant.Modules.REST.LocalService;
using Sector7.Restaurant.Biz;
using Sector7.Framework.Utility;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;

namespace Sector7.Restaurant.Modules.REST.Windows
{
    /// <summary>
    /// AddEmployeeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddEmployeeWindow : AddModeWindowBase
    {
        public AddEmployeeWindow(RS_EmployeeGroup selectedGroup, List<RS_EmployeeGroup> groups)
        {
            InitializeComponent();

            cbbGroup.ItemsSource = groups;
            cbbGroup.SelectedItem = selectedGroup;
        }

        public RS_Employee Employee
        {
            get { return m_entity as RS_Employee; }
            set { m_entity = value; }
        }

        protected override void InitializeForCreating()
        {
            Employee = new RS_Employee();
            Employee.IsValid = true;
            Employee.Birthday = new DateTime(2008, 10, 18);
        }

        protected override void InitializeForUpdating()
        {
            btnContinue.Visibility = Visibility.Hidden;
        }

        protected override void AddData()
        {
            Employee.Password = txbPassword.Password;

            switch (m_crud)
            {
                case CrudType.Create:
                    Employee.UniqueId = Aide.LocalClient.Invoke<long>(
                        service => service.AddEmployee(Employee));
                    break;
                case CrudType.Update:
                    EntityDelta delta = ClientUtil.CreateDelta<EntityDelta>(typeof(RS_Employee));
                    delta.Conditions.Add("UniqueId", Employee.UniqueId);
                    delta.DeltaProperties.Add("LastName", Employee.LastName);
                    delta.DeltaProperties.Add("FirstName", Employee.FirstName);
                    delta.DeltaProperties.Add("Birthday", Employee.Birthday);
                    delta.DeltaProperties.Add("Gender", Employee.Gender);
                    delta.DeltaProperties.Add("UserName", Employee.UserName);
                    delta.DeltaProperties.Add("Password", Employee.Password);
                    delta.DeltaProperties.Add("MobilePhone", Employee.MobilePhone);
                    delta.DeltaProperties.Add("Telephone", Employee.Telephone);
                    delta.DeltaProperties.Add("PersonalId", Employee.PersonalId);
                    delta.DeltaProperties.Add("Address", Employee.Address);
                    delta.DeltaProperties.Add("Residence", Employee.Residence);
                    delta.DeltaProperties.Add("BasicSalary", Employee.BasicSalary);
                    delta.DeltaProperties.Add("GroupId", Employee.GroupId);

                    Aide.LocalClient.Invoke<int>(
                        service => service.SetEmployee(delta));
                    break;
                default:
                    break;
            }

            base.AddData();
        }
    }
}
